﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Xml;
using System.Web;
using System.Text;


namespace idseefeld.com.images
{
    public class IDseefeldImagesConfig
    {
        public bool MediaEventsEnabled { get; private set; }
        public string DocumentTypes { get; private set; }
        public int ReducedWidth { get; private set; }
        public int ReducedHeight { get; private set; }

        public IDseefeldImagesConfig()
        {
            Init();
        }

        private void Init()
        {
            MediaEventsEnabled = false;
            StringBuilder sb = new StringBuilder();
            XmlDocument settingsXml = new XmlDocument();
            settingsXml.Load(HttpContext.Current.Server.MapPath("~/config/idseefeldImages.config"));


            foreach (XmlNode section in settingsXml.DocumentElement.SelectNodes("//mediaEvents"))
            {
                MediaEventsEnabled = section.Attributes["enabled"].Value.ToLower().Equals("true");
                int tmp = 0;
                int.TryParse(section.Attributes["reducedWidth"].Value, out tmp);
                ReducedWidth = tmp;
                tmp = 0;
                int.TryParse(section.Attributes["reducedHeight"].Value, out tmp);
                ReducedHeight = tmp;
                foreach (XmlNode entry in section.SelectNodes("./mediaType"))
                {
                    sb.Append(getFirstText(entry) + ",");
                }
                DocumentTypes = sb.ToString();
            }
        }
        private string getFirstText(XmlNode node)
        {
            foreach (XmlNode n in node.ChildNodes)
            {
                if (n.NodeType == XmlNodeType.Text)
                    return n.Value;
            }

            return "";
        }

    }
}
